/**
 * 
 */
package com.orange.controller.manage;

import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.controller.BaseController;
import com.orange.dbhelp.page.Page;
import com.orange.handler.expert.AuthAnnouncementService;
import com.orange.modules.entity.manage.AuthAnnouncement;
import com.orange.modules.entity.manage.AuthSysDownload;
import com.orange.util.ReturnCode;

/**
 * @author gaogs
 *
 * 2016-12-15
 */
@Controller
@RequestMapping("/authAnnouncement")
public class AuthAnnouncementController extends BaseController{

	private static final Logger logger = Logger.getLogger(AuthAnnouncementController.class);
	
	@Autowired
	private AuthAnnouncementService authAnnouncementService;
	
	/**
	 * 新建/修改公告
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/saveAuthAnnouncementInfo" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> saveAuthAnnouncementInfo(HttpServletRequest request,@RequestBody Map<String,String> map) throws SQLException{
		
		try{
			String type=map.get("type");//
			String title=map.get("title");
			String content=map.get("content");
			String pid=map.get("pid");//修改时传此值
			
			if(StringUtils.isBlank(title)||StringUtils.isBlank(content)){
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "参数存在空值");
			}
			String nowTime=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
			AuthAnnouncement a=null;
			if(StringUtils.isBlank(pid)){
				if(StringUtils.isBlank(type)){
					return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "公告类型不可为空");
				}
				a=new AuthAnnouncement();
				a.setCreateTime(nowTime);
				a.setType(type);
			}else{
				a=authAnnouncementService.queryAuthAnnouncementById(pid);
				if(null==a){
					return toResponse(null, ReturnCode.USERINFO_NOT_EXIST, "该公告信息不存在");
				}
			}
			a.setTitle(title);
			a.setContent(content);
			authAnnouncementService.saveAuthAnnouncementInfo(a);
			return toResponse(null, ReturnCode.SUCCESS, "新建公告成功");
		}catch(Exception e){
			logger.info("新建公告失败", e);
			return toResponse(null,ReturnCode.FAIL,"新建公告失败");
		}
	}
	/**
	 * 根据公告id、查询公告信息
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAuthAnnouncementInfoByid" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> queryAuthAnnouncementInfoByid(HttpServletRequest request,@RequestParam String pid) throws SQLException{
		
		try{
			
			if(StringUtils.isBlank(pid)){
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "参数存在空值");
			}
			AuthAnnouncement a=authAnnouncementService.queryAuthAnnouncementById(pid);
			return toResponse(a, ReturnCode.SUCCESS, "根据公告id、查询公告信息成功");
		}catch(Exception e){
			logger.info("根据公告id、查询公告信息失败", e);
			return toResponse(null,ReturnCode.FAIL,"根据公告id、查询公告信息失败");
		}
	}
	
	/**
	 * 删除公告
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/deleteAuthAnnouncementInfo" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<String> saveAuthAnnouncementInfo(HttpServletRequest request,@RequestParam String pid) throws SQLException{
		
		try{
			if(StringUtils.isBlank(pid)){
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "参数存在空值");
			}
			authAnnouncementService.deleteAuthAnnouncementInfo(pid);
			return toResponse(null, ReturnCode.SUCCESS, "删除公告成功");
		}catch(Exception e){
			logger.info("删除公告失败", e);
			return toResponse(null,ReturnCode.FAIL,"删除公告失败");
		}
	}
	
	/**
	 * 分页查询公告
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/queryAuthSysDownloadInfoPage" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> queryAuthSysDownloadInfoPage(HttpServletRequest request,@RequestBody Map<String,String> map) throws SQLException{
		
		try{
			String pageNumber=map.get("pageNumber");
			String pageSize=map.get("pageSize");
			
			if(StringUtils.isBlank(pageNumber)||StringUtils.isBlank(pageSize)){
				return toResponse(null, ReturnCode.PARAMETER_NOT_NULL, "参数存在空值");
			}
			Page<AuthAnnouncement> page=authAnnouncementService.queryAuthSysDownloadInfoPage(map);
			return toResponse(page, ReturnCode.SUCCESS, "分页查询公告成功");
		}catch(Exception e){
			logger.info("分页查询公告失败", e);
			return toResponse(null,ReturnCode.FAIL,"分页查询公告失败");
		}
	}

}
